package com.bahiaapk;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import blueprint.software.blueframe.synchronize.HTTPConnect;

import com.bahiaapk.util.BahiaUtil;
import com.google.android.gcm.GCMRegistrar;

public class SplashActivity extends Activity{
	public static String TAG = "SplashActivity";
	private ProgressDialog dialog;
	private Context context;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.splash);     
        context = this;
        
        dialog = ProgressDialog.show(this,null,null);
        dialog.setContentView(R.layout.splash);
        context = this;
        
        
        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        
        int heightScreen = display.getHeight();
        int widthScreen = display.getWidth();
        ImageView image = (ImageView) dialog.findViewById(R.id.ballIMG);
    	RelativeLayout.LayoutParams defaultLayout = (RelativeLayout.LayoutParams) image.getLayoutParams();
    	defaultLayout.topMargin=(int) Math.round(heightScreen*0.55);
    	defaultLayout.leftMargin=(int) Math.round(widthScreen*0.14);
        image.setLayoutParams(defaultLayout);
        
        
        Synchronize synchronize = new Synchronize();
 		
 		Handler handler = new Handler(){
 			@Override
 			public void handleMessage(Message msg)
 			{
 				Bundle data = msg.getData();
 				Boolean status = data.getBoolean("status");
 				
 				if(status){
 					
 					 try {
 						 
 						GCMRegistrar.checkDevice(context);
 	                    GCMRegistrar.checkManifest(context);
 	                    final String regId = GCMRegistrar.getRegistrationId(context);
 	                    
 	                    if (regId.equals("")|| regId==null) {
 	                    	Log.v(TAG, "Registering...");
 	                    	GCMRegistrar.register(context, BahiaUtil.senderId);
 	                    } else {
 	                    	Log.v(TAG, "Already registered");
 	                    //	BahiaUtil.setToken(regId);
 	                    }
 	                    
 	                   nextPage();
 	      			   dialog.dismiss();

					} catch (Throwable e) {
                    	Log.v(TAG, e.getMessage());
					}
 				}
 			}
 			
 		};
 		
 		if(BahiaUtil.haveNetworkConnection(this)){
         	Log.v(TAG, "Synchronizing all data...");
 			synchronize.allData(this, handler);
 		}else{
			Toast.makeText(this, "Internet offline", Toast.LENGTH_LONG).show();
			nextPage();
			dialog.dismiss();
		}

    }
    
    private void nextPage(){
    	Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
    }
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	GCMRegistrar.onDestroy(this);
    }
    
   private class Synchronize implements Runnable {
    	
    	private Handler handler;
    	private HTTPConnect connect;


    	private final Thread thread = new Thread(this);
    	
    	public void allData(Context context, Handler handler){
    		this.handler = handler;    		
    		thread.start();
    	}
    	
    	
    	@Override
    	public void run() {
    		Bundle data = new Bundle();
    		
    		data.putBoolean("status", true);
			
    		try {
    			thread.sleep(4000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
    		
    		Message msg = new Message();
    		msg.setData(data);
    						
    		handler.sendMessage(msg);
    	}
   }
}
